<template>
    <el-dialog :title="title" custom-class="el-dialog-600" v-model="visible">
        <el-form :model="form" :rules="rules" ref="formRef" label-width="80px">
            <el-form-item label="角色名" prop="name">
                <el-col :span="23">
                    <el-input clearable v-model="form.name" placeholder="请输入菜单名称"></el-input>
                </el-col>
            </el-form-item>

            <el-form-item label="状态" prop="status">
                <el-col :span="23">
                    <el-radio
                        clearable
                        v-model="form.status"
                        v-for="item in statusRadio"
                        :key="item.id"
                        :label="item.id"
                    >
                        {{ item.name }}
                    </el-radio>
                </el-col>
            </el-form-item>

            <el-form-item label="备注" prop="remarks">
                <el-col :span="23">
                    <el-input type="textarea" v-model="form.remarks"></el-input>
                </el-col>
            </el-form-item>
        </el-form>

        <template #footer>
            <el-button @click="visible = false"> 取 消 </el-button>
            <el-button type="primary" :loading="loading" @click="onSubmit"> 确 定 </el-button>
        </template>
    </el-dialog>
</template>

<script>
import { getCurrentInstance, ref, reactive, watch } from 'vue';
import Options from '@/global/local-data/system';
import { adminRole as Api } from 'api';

export default {
    setup() {
        const { proxy } = getCurrentInstance();

        let id = ref(null); // 用户id
        let loading = ref(false); //等待按钮
        let visible = ref(false); //弹窗开关
        let title = ref(null); // 标题
        let statusRadio = Options.role.statusRadio;

        // 表单
        let form = reactive({
            name: '',
            status: 1,
            remarks: ''
        });
        // 验证
        let rules = {
            name: [
                { required: true, message: '请输入角色名', trigger: 'change' },
                { min: 2, max: 16, message: '字符长度2到16个字符', trigger: 'change' }
            ]
        };

        // 初始化
        const init = () => {
            title.value = id.value ? '编辑' : '新增';

            if (id.value) getById();
        };
        // 获取详情
        const getById = async () => {
            const { code, data } = await proxy.$http.get(Api.getById, {
                params: { id: id.value }
            });

            if (code === 200) {
                form.name = data.name;
                form.status = data.status;
                form.remarks = data.remarks;
            }
        };
        // 提交表单
        const onSubmit = async () => {
            // 表单验证，通过就禁止按钮
            await proxy.$refs['formRef'].validate((valid) => {
                // 禁止按钮
                if (valid) loading.value = true;
            });

            if (loading.value) {
                let url = id.value ? Api.update : Api.add;

                let params = {
                    name: form.name,
                    status: form.status,
                    remarks: form.remarks
                };

                if (id.value) params.id = id.value;

                const { code, msg } = await proxy.$http.post(url, params);

                if (code === 200) {
                    proxy.$msg.success(msg);
                    visible.value = false;
                    proxy.$parent.getList();
                } else {
                    proxy.$msg.error(msg);
                }

                loading.value = false;
            }
        };

        // 监听窗口开关
        watch(visible, (newVal, oldVal) => {
            newVal ? init() : proxy.$refs['formRef'].resetFields();
        });

        return {
            id,
            loading,
            visible,
            title,
            statusRadio,
            form,
            rules,
            onSubmit
        };
    }
};
</script>
